package com.hdyg.sixg.activity.trans;

import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.allen.library.SuperTextView;
import com.hdyg.sixg.R;
import com.hdyg.sixg.activity.common.BaseActivity;
import com.hdyg.sixg.entry.BuyBean;
import com.hdyg.sixg.mvp.Contrant.CBuy;
import com.hdyg.sixg.mvp.base.BasePresenter;
import com.hdyg.sixg.mvp.base.BaseUrl;
import com.hdyg.sixg.mvp.base.GetParamUtil;
import com.hdyg.sixg.mvp.base.SpMsg;
import com.hdyg.sixg.mvp.presenter.PBuy;
import com.hdyg.sixg.util.StringUtil;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;

import butterknife.BindView;

/**
 * @author CZB
 * @describe 购买界面
 * @time 2019/4/8 14:28
 */
public class BuyActivity extends BaseActivity implements CBuy.IVBuy {

    @BindView(R.id.ll_top_left)
    LinearLayout llTopLeft;
    @BindView(R.id.tvTopTitle)
    TextView tvTopTitle;
    @BindView(R.id.sv_limit_number)
    SuperTextView svLimitNumber;
    @BindView(R.id.et_buy_number)
    EditText etBuyNumber;
    @BindView(R.id.sv_valuation)
    SuperTextView svValuation;
    @BindView(R.id.sv_all_valuation)
    SuperTextView svAllValuation;
    @BindView(R.id.et_pay_pwd)
    EditText etPayPwd;
    @BindView(R.id.bt_sure)
    Button btSure;
    @BindView(R.id.srl_refresh)
    SmartRefreshLayout srlRefresh;

    private String orderid, amount, trapass;
    private CBuy.IPBuy mPresenter;
    private BuyBean dataBean;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_buy;
    }

    @Override
    protected void initView() {
        onClickListener();
        tvTopTitle.setText(R.string.trans_item_5);
        orderid = getIntent().getStringExtra(SpMsg.INTENT_STR);
    }

    private void onClickListener() {
        srlRefresh.setOnRefreshListener(refreshLayout -> {
            srlRefresh.finishRefresh(2000);//传入false表示刷新失败
            initData();
        });
        etBuyNumber.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                amount = s.toString();
                if (!TextUtils.isEmpty(amount)) {
                    double str = Double.valueOf(amount) * Double.valueOf(dataBean.getPrice());
                    svAllValuation.setRightString(StringUtil.dealFloatStr(str));
                }else {
                    svAllValuation.setRightString("0");
                }
            }
        });

        llTopLeft.setOnClickListener(v -> finish());

        btSure.setOnClickListener(v -> {
            toastNotifyShort("确认购买");
            trapass = etPayPwd.getText().toString().trim();
            if (TextUtils.isEmpty(amount)) {
                toastNotifyShort(R.string.trans_buy_number_hint);
                return;
            }
            if (TextUtils.isEmpty(trapass)) {
                toastNotifyShort(R.string.pay_pwd_hint);
                return;
            }
            mPresenter.pGetApplyBuy(BaseUrl.APPLY_BUY_URL, GetParamUtil.getApplyBuyParam(amount, trapass, orderid));
        });
    }

    @Override
    protected void initData() {
        mPresenter.pGetInit(BaseUrl.BUY_URL, GetParamUtil.getBuyParam(orderid));
    }

    @Override
    protected void createPresenter() {
        mPresenter = new PBuy(this);
    }

    @Override
    public void vGetInitSuccess(BuyBean dataBean) {
        this.dataBean = dataBean;
        svLimitNumber.setRightString(dataBean.getBuy_amount());
        svValuation.setRightString(dataBean.getPrice());
    }

    @Override
    public void vGetApplyBuySuccess(String dataBean) {
        toastNotifyShort(dataBean);
    }
}
